﻿using System.Data;
using Stock.Common;
using Stock.Dal;
using Stock.Model;

namespace Stock.Bl
{
    /// <summary>
    /// Description of StockOptDetailBl.
    /// </summary>
    public class StockOptDetailBl
    {
        private readonly StockOptDetailDal stockOptDetailDal = new StockOptDetailDal();
        private readonly MyStockDal myStockDal = new MyStockDal();
        private readonly ParameterTbDal parameterTbDal = new ParameterTbDal();
        private readonly StockInfoDal stockInfoDal = new StockInfoDal();
        private const decimal tax = 0.1M;

        public StockOptDetailBl()
        {
        }

        #region Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(StockOptDetailEntity model)
        {
            return stockOptDetailDal.Add(model);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public StockOptDetailEntity GetModel(int Detail_Cd)
        {
            return stockOptDetailDal.GetModel(Detail_Cd);
        }

        /// <summary>
        /// 得到一个DataSet
        /// </summary>
        /// <param name="str">条件</param>
        /// <returns></returns>
        public DataSet GetDataList(string str)
        {
            return stockOptDetailDal.GetList(str);
        }

        /// <summary>
        /// 得到一个类别的DataSet
        /// </summary>
        /// <param name="str">条件</param>
        /// <returns></returns>
        public DataSet GetTypeDataList(string str)
        {
            return parameterTbDal.GetList(str);
        }

        #endregion Method

        public void Update(StockOptDetailEntity stockOptDetailEntity)
        {
            stockOptDetailDal.Update(stockOptDetailEntity);
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="stockOptDetailEntity"></param>
        internal void UpdateStockOptDetail(StockOptDetailEntity stockOptDetailEntity, StockOptDetailEntity oldOptDetailEntity)
        {
            //throw new System.NotImplementedException();
        }

        /// <summary>
        /// Add
        /// </summary>
        /// <param name="stockOptDetailEntity"></param>
        internal void AddStockOptDetail(StockOptDetailEntity stockOptDetailEntity)
        {
            stockOptDetailDal.Add(stockOptDetailEntity);
            MyStockEntity myStockEntity = new MyStockEntity();
            MyStockEntity oldMyStockEntity = new MyStockEntity();

            if (stockOptDetailEntity.Opt_type.Equals("1"))
            {
                #region 买入

                if (!myStockDal.Exists(stockOptDetailEntity.S_Code, Const.USER_CODE))
                {
                    //不存在
                    myStockEntity.Copies = stockOptDetailEntity.Opt_Copies;
                    myStockEntity.FeeAmount = stockOptDetailEntity.Opt_Fee;
                    myStockEntity.InAmount = stockOptDetailEntity.Opt_Sum;
                    myStockEntity.LastPrice = stockOptDetailEntity.Opt_Price;
                    myStockEntity.OutAmount = 0;
                    myStockEntity.S_Code = stockOptDetailEntity.S_Code;
                    myStockDal.Add(myStockEntity);
                }
                else
                {
                    oldMyStockEntity = myStockDal.GetModel(stockOptDetailEntity.Opt_Code);
                    myStockEntity.Copies = oldMyStockEntity.Copies + stockOptDetailEntity.Opt_Copies;
                    myStockEntity.FeeAmount = oldMyStockEntity.FeeAmount + stockOptDetailEntity.Opt_Fee;
                    myStockEntity.InAmount = oldMyStockEntity.InAmount + stockOptDetailEntity.Opt_Sum;
                    myStockEntity.LastPrice = stockOptDetailEntity.Opt_Price;
                    myStockEntity.OutAmount = oldMyStockEntity.OutAmount;
                    myStockEntity.S_Code = oldMyStockEntity.S_Code;
                    myStockDal.Update(myStockEntity);
                }

                #endregion 买入
            }
            else if (stockOptDetailEntity.Opt_type.Equals("2"))
            {
                #region 卖出

                oldMyStockEntity = myStockDal.GetModel(stockOptDetailEntity.Opt_Code);
                myStockEntity.Copies = oldMyStockEntity.Copies - stockOptDetailEntity.Opt_Copies;
                myStockEntity.FeeAmount = oldMyStockEntity.FeeAmount + stockOptDetailEntity.Opt_Fee;
                myStockEntity.InAmount = oldMyStockEntity.InAmount;
                myStockEntity.LastPrice = stockOptDetailEntity.Opt_Price;
                if (oldMyStockEntity.OutAmount == null)
                {
                    myStockEntity.OutAmount = stockOptDetailEntity.Opt_Sum;
                }
                else
                {
                    myStockEntity.OutAmount = oldMyStockEntity.OutAmount + stockOptDetailEntity.Opt_Sum;
                }

                myStockEntity.S_Code = oldMyStockEntity.S_Code;
                myStockDal.Update(myStockEntity);

                #endregion 卖出
            }
            else if (stockOptDetailEntity.Opt_type.Equals("3"))
            {
                #region 分红

                oldMyStockEntity = myStockDal.GetModel(stockOptDetailEntity.Opt_Code);
                myStockEntity.Copies = oldMyStockEntity.Copies;
                myStockEntity.FeeAmount = oldMyStockEntity.FeeAmount + oldMyStockEntity.Copies / 10 * stockOptDetailEntity.Opt_Fee * tax;//每10份分红额 * 份数 * ( 税10%)
                myStockEntity.InAmount = oldMyStockEntity.InAmount;
                //myStockEntity.LastPrice = stockOptDetailEntity.Opt_Price;
                myStockEntity.LastPrice = oldMyStockEntity.LastPrice * (oldMyStockEntity.LastPrice * 10 / (oldMyStockEntity.LastPrice * 10 + stockOptDetailEntity.Opt_Fee));
                myStockEntity.OutAmount = oldMyStockEntity.OutAmount + oldMyStockEntity.Copies / 10 * stockOptDetailEntity.Opt_Fee;//每10份分红额 * 份数

                myStockEntity.S_Code = oldMyStockEntity.S_Code;
                myStockDal.Update(myStockEntity);

                #endregion 分红
            }
            else if (stockOptDetailEntity.Opt_type.Equals("4"))
            {
                #region 转增股

                //股票红利税20%，不过目前减半征收，也就是说只收10%，由上市公司代收，也就是分到账户的钱就是税后的了，投资者无需另行缴纳。转增股本由于资产没有转成现金所以不收税。
                //例如，某公司分红方案为10送2，派1，转增5，由于送股按面值1元计，所以1股收税0.1元，送两股收税0.2元，派息1元收税0.1元，即总共需缴税0.3元，那么每10股送派转到账的实际股数和现金为2+5=7股，1.0-0.2-0.1=0.7元，
                //于是，你的账户假设股权登记日持有100股，那么等到账之后，账户上会变成：170股+7元现金。
                oldMyStockEntity = myStockDal.GetModel(stockOptDetailEntity.Opt_Code);
                //myStockEntity.Copies = oldMyStockEntity.Copies * (1 + stockOptDetailEntity.Opt_Fee / 10);
                myStockEntity.FeeAmount = oldMyStockEntity.FeeAmount;
                myStockEntity.InAmount = oldMyStockEntity.InAmount;

                myStockEntity.LastPrice = oldMyStockEntity.LastPrice * 10 / (10 + stockOptDetailEntity.Opt_Fee);
                myStockEntity.OutAmount = oldMyStockEntity.OutAmount;

                myStockEntity.S_Code = oldMyStockEntity.S_Code;
                myStockDal.Update(myStockEntity);

                #endregion 转增股
            }
            else if (stockOptDetailEntity.Opt_type.Equals("5"))
            {
                #region 送股

                //股票红利税20%，不过目前减半征收，也就是说只收10%，由上市公司代收，也就是分到账户的钱就是税后的了，投资者无需另行缴纳。转增股本由于资产没有转成现金所以不收税。
                //例如，某公司分红方案为10送2，派1，转增5，由于送股按面值1元计，所以1股收税0.1元，送两股收税0.2元，派息1元收税0.1元，即总共需缴税0.3元，那么每10股送派转到账的实际股数和现金为2+5=7股，1.0-0.2-0.1=0.7元，
                //于是，你的账户假设股权登记日持有100股，那么等到账之后，账户上会变成：170股+7元现金。
                oldMyStockEntity = myStockDal.GetModel(stockOptDetailEntity.Opt_Code);
                //myStockEntity.Copies = oldMyStockEntity.Copies * (1 + stockOptDetailEntity.Opt_Fee / 10);
                myStockEntity.FeeAmount = oldMyStockEntity.FeeAmount + stockOptDetailEntity.Opt_Price * tax * stockOptDetailEntity.Opt_Fee / 10;
                myStockEntity.InAmount = oldMyStockEntity.InAmount;

                myStockEntity.LastPrice = oldMyStockEntity.LastPrice * 10 / (10 + stockOptDetailEntity.Opt_Fee);
                myStockEntity.OutAmount = oldMyStockEntity.OutAmount;

                myStockEntity.S_Code = oldMyStockEntity.S_Code;
                myStockDal.Update(myStockEntity);

                #endregion 送股
            }
        }

        public StockInfoEntity GetStockByKey(string p)
        {
            string wherestr = "S_Code='" + p + "' or S_PYM='" + p.ToUpper() + "'";
            StockInfoEntity stockInfoEntity = stockInfoDal.GetModelByWhere(wherestr);
            return stockInfoEntity;
        }
    }
}